<?php

namespace App\Http;

use App\Admin\Arole;
use App\Admin\Rule;
use Illuminate\Contracts\View\View;
use Illuminate\Support\ServiceProvider;


class Middlewal
{
    //
    public function compose(View $view)
    {
        $admin = session()->get('admin');
        if(!$admin){
            return redirect('admin/login');
        }
        $id = $admin[0]->id;
        //查询用户当前登录用户的权限
        $permissions = Arole::select('power.*')
            ->LeftJoin('rpower', 'rpower.role_id', 'arole.role_id')
            ->LeftJoin('power', 'power.id', 'rpower.rule_id')
            ->where('arole.user_id', $id)
            ->where('power.is_menu', 0)
            ->groupBy('power.id')
            ->get();
//        dd($permissions);
        ///得到数组的结构
        $tree = array();
            foreach ($permissions as $permission) {
                //取出来顶级的元素
                if ($permission->pid == 0) {
                    //再次循环数组
                    foreach ($permissions as $v)
//                    dump($v->pid);
                        if ($permission->id == $v->pid) {
                            $tree[$permission->display_name][] = $v;
                        }
                }
        }
//            dd($tree);
        // 使用基于闭包的composers...
        view()->composer('layouts.admin', function ($view) use ($tree) {
            $view->with('tree', $tree);
        });
    }
}